﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Data;
using System.Data.SqlClient;

namespace Portal.CMS
{
    public class AddLawDocumentItem : WebPart
    {
      
        public override string WebPartId
        {
            get
            {
                return "AddLawDocumentItem";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Thêm tài liệu mới");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddLawDocumentItem));
            string LawDocumentId = (string)Environments.Request("LawDocumentId");
            PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocument");
            LawDocumentCls LawDocumentCls = new LawDocumentBLL().CreateModel(LawDocumentId);
            Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddItem", LawDocumentCls.LawDocumentId, LawDocumentCls.Owner, true);

            string AllSubject = ResourceBLL.GetText(WebPartId, "SelectSubject", "== Chọn từ danh sách ==");
            DataTable dtLawDocumentOffice = new LawDocumentOfficeBLL().ReadingLawDocumentOffice("");
            string SelectLawDocumentOffice = "<select id=drpSelectLawDocumentOffice>";
            SelectLawDocumentOffice += "<option value=\"\">" + AllSubject + "</option>";
            for (int iIndex = 0; iIndex < dtLawDocumentOffice.Rows.Count; iIndex++)
            {
                string LawDocumentOfficeId = (string)dtLawDocumentOffice.Rows[iIndex]["LawDocumentOfficeId"];
                string Subject = (string)dtLawDocumentOffice.Rows[iIndex]["Subject"];
                SelectLawDocumentOffice += "<option value=\"" + LawDocumentOfficeId + "\">" + Subject + "</option>";
            }
            SelectLawDocumentOffice += "</select>";



            DataTable dtLawDocumentType = new LawDocumentTypeBLL().ReadingLawDocumentType("");
            string SelectLawDocumentType = "<select id=drpSelectLawDocumentType>";
            SelectLawDocumentType += "<option value=\"\">" + AllSubject + "</option>";
            for (int iIndex = 0; iIndex < dtLawDocumentType.Rows.Count; iIndex++)
            {
                string LawDocumentTypeId = (string)dtLawDocumentType.Rows[iIndex]["LawDocumentTypeId"];
                string Subject = (string)dtLawDocumentType.Rows[iIndex]["Subject"];
                SelectLawDocumentType += "<option value=\"" + LawDocumentTypeId + "\">" + Subject + "</option>";
            }
            SelectLawDocumentType += "</select>";

            DataTable dtLawDocumentSigner = new LawDocumentSignerBLL().ReadingLawDocumentSigner("");
            string SelectLawDocumentSigner = "<select id=drpSelectLawDocumentSigner>";
            SelectLawDocumentSigner += "<option value=\"\">" + AllSubject + "</option>";
            for (int iIndex = 0; iIndex < dtLawDocumentSigner.Rows.Count; iIndex++)
            {
                string LawDocumentSignerId = (string)dtLawDocumentSigner.Rows[iIndex]["LawDocumentSignerId"];
                string Subject = (string)dtLawDocumentSigner.Rows[iIndex]["Subject"];
                SelectLawDocumentSigner += "<option value=\"" + LawDocumentSignerId + "\">" + Subject + "</option>";
            }
            SelectLawDocumentSigner += "</select>";

            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "LawDocument.js") +
                "<input id=txtLawDocumentId type=hidden value=\"" + LawDocumentId + "\">" +
                "<table>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Code", "Số") + ":</td>" +
                "           <td><input class=cssTextBoxDialog  id=txtCode></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Subject", "Tiêu đề") + ":</td>" +
                "           <td><input class=cssTextBoxDialog  id=txtSubject></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Keyword", "Từ khóa") + ":</td>" +
                "           <td><input class=cssTextBoxDialog  id=txtKeyword></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Description", "Mô tả") + ":</td>" +
                "           <td><textarea class=cssBodyLawDocument id=txtBody></textarea></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Signer", "Người ký") + ":</td>" +
                "           <td><input class=cssFilterTextBox id=txtFilterSigner><input type=button value=\"...\" onclick=detectSigner();><span id=divSigner>" + SelectLawDocumentSigner + "</span></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "Office", "Đơn vị phát hành") + ":</td>" +
                "           <td><input class=cssFilterTextBox id=txtFilterOffice><input type=button value=\"...\" onclick=detectOffice();><span id=divOffice>" + SelectLawDocumentOffice + "</span></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "DocType", "Loại văn bản") + ":</td>" +
                "           <td><input class=cssFilterTextBox id=txtFilterDoctype><input type=button value=\"...\" onclick=detectDocType();><span id=divDocumentType>" + SelectLawDocumentType + "</span></td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "ReleaseDate", "Ngày ban hành") + ":</td>" +
                "           <td>" + Environment.LookUpUtilities.RemoveReadOnly(Environment.LookUpUtilities.GenerateLookUpDate("txtReleasedDate")) +"</td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "EffectedDate", "Ngày hiệu lực") + ":</td>" +
                "           <td>" + Environment.LookUpUtilities.RemoveReadOnly(Environment.LookUpUtilities.GenerateLookUpDate("txtEffectedDate")) + "</td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "ExpiredDate", "Ngày hết hạn") + ":</td>" +
                "           <td>" + Environment.LookUpUtilities.RemoveReadOnly(Environment.LookUpUtilities.GenerateLookUpDate("txtExpiredDate")) + "</td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td>" + ResourceBLL.GetText(WebPartId, "EntryDate", "Ngày nhập") + ":</td>" +
                "           <td>" + Environment.LookUpUtilities.RemoveReadOnly(Environment.LookUpUtilities.GenerateLookUpDate("txtEntryDate",System.DateTime.Now)) + "</td>" +
                "       </tr>" +
                "       <tr>" +
                "           <td></td>" +
                "           <td><input onclick=javascript:SaveAddLawDocumentItem('"+LawDocumentId+"'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "saveButton", "Ghi lại") + "\"><input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Thoát") + "\"></td>" +
                "       </tr>" +
                "</table>";
            RetHtml = Environments.GetFormDialog(RetHtml, Title);
            return RetHtml;
        }


        
        [AjaxPro.AjaxMethod]
        public string Save(
            string LawDocumentId,
            string Code,
            string Subject,
            string Keyword,
            string Body,
            string LawDocumentTypeId,
            string LawDocumentOfficeId,
            string LawDocumentSignerId,
            string ReleasedDate,
            string EffectedDate,
            string ExpiredDate,
            string EntryDate)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocument");
                LawDocumentCls LawDocumentCls = new LawDocumentBLL().CreateModel(LawDocumentId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddItem", LawDocumentCls.LawDocumentId, LawDocumentCls.Owner, true);

                if(Code.Trim().Length==0)throw new Exception(ResourceBLL.GetText(WebPartId,"CodeIsMissing","Chưa nhập số văn bản!"));
                if(Subject.Trim().Length==0)throw new Exception(ResourceBLL.GetText(WebPartId,"SubjectIsMissing","Chưa tiêu đề văn bản!"));
                if (LawDocumentTypeId.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "LawDocumentTypeIsMissing", "Chưa chọn loại văn bản!"));
                if (LawDocumentSignerId.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "LawDocumentSignerIsMissing", "Chưa chọn người ký văn bản!"));
                if (LawDocumentOfficeId.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "LawDocumentOfficeIsMissing", "Chưa chọn đơn vị phát hành!"));

                if (ReleasedDate.Trim().Length > 0)
                {
                    if (Environment.FuncUtilities.checkVnDate(ReleasedDate) == false)
                    {
                        throw new Exception(ResourceBLL.GetText(WebPartId, "InvalidReleasedDate", "Ngày phát hành nhập sai"));
                    }
                }
                if (EffectedDate.Trim().Length > 0)
                {
                    if (Environment.FuncUtilities.checkVnDate(EffectedDate) == false)
                    {
                        throw new Exception(ResourceBLL.GetText(WebPartId, "InvalidEffectedDate", "Ngày hiệu lực nhập sai"));
                    }
                }
                if (ExpiredDate.Trim().Length > 0)
                {
                    if (Environment.FuncUtilities.checkVnDate(ExpiredDate) == false)
                    {
                        throw new Exception(ResourceBLL.GetText(WebPartId, "InvalidExpiredDate", "Ngày hết hạn nhập sai"));
                    }
                }
                if (EntryDate.Trim().Length > 0)
                {
                    if (Environment.FuncUtilities.checkVnDate(EntryDate) == false)
                    {
                        throw new Exception(ResourceBLL.GetText(WebPartId, "InvalidEntryDate", "Ngày tạo tài liệu nhập sai"));
                    }
                }
                string LawDocumentItemId = new LawDocumentBLL().CreateLawDocumentItem(null, LawDocumentId, LawDocumentCls.belongSite, Code, Subject, Keyword, Body, LawDocumentTypeId, LawDocumentOfficeId, LawDocumentSignerId, ReleasedDate, EffectedDate, ExpiredDate, EntryDate);
                return LawDocumentItemId;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetUpdateContentLawDocumentItemUrl(string LawDocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
                LawDocumentItemCls LawDocumentItemCls = new LawDocumentBLL().CreateModelItem(LawDocumentItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateItem", LawDocumentItemId, LawDocumentItemCls.Owner, true);

                string UpdateLawDocumentItemUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UploadLawDocumentItem().WebPartId),
                        new ParamCls("LawDocumentItemId",LawDocumentItemId)
                    });
                return UpdateLawDocumentItemUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerDetectSigner(string Keyword)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                DataTable dtLawDocumentSigner = new LawDocumentSignerBLL().ReadingLawDocumentSigner(Keyword);
                string SelectLawDocumentSigner = "<select id=drpSelectLawDocumentSigner>";
                //SelectLawDocumentSigner += "<option value=\"\">" + AllSubject + "</option>";
                for (int iIndex = 0; iIndex < dtLawDocumentSigner.Rows.Count; iIndex++)
                {
                    string LawDocumentSignerId = (string)dtLawDocumentSigner.Rows[iIndex]["LawDocumentSignerId"];
                    string Subject = (string)dtLawDocumentSigner.Rows[iIndex]["Subject"];
                    SelectLawDocumentSigner += "<option value=\"" + LawDocumentSignerId + "\">" + Subject + "</option>";
                }
                SelectLawDocumentSigner += "</select>";
                return SelectLawDocumentSigner;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerDetectOffice(string Keyword)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                DataTable dtLawDocumentOffice = new LawDocumentOfficeBLL().ReadingLawDocumentOffice(Keyword);
                string SelectLawDocumentOffice = "<select id=drpSelectLawDocumentOffice>";
                //SelectLawDocumentOffice += "<option value=\"\">" + AllSubject + "</option>";
                for (int iIndex = 0; iIndex < dtLawDocumentOffice.Rows.Count; iIndex++)
                {
                    string LawDocumentOfficeId = (string)dtLawDocumentOffice.Rows[iIndex]["LawDocumentOfficeId"];
                    string Subject = (string)dtLawDocumentOffice.Rows[iIndex]["Subject"];
                    SelectLawDocumentOffice += "<option value=\"" + LawDocumentOfficeId + "\">" + Subject + "</option>";
                }
                SelectLawDocumentOffice += "</select>";
                return SelectLawDocumentOffice;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerDetectDocumentType(string Keyword)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                DataTable dtLawDocumentType = new LawDocumentTypeBLL().ReadingLawDocumentType(Keyword);
                string SelectLawDocumentType = "<select id=drpSelectLawDocumentType>";
                //SelectLawDocumentType += "<option value=\"\">" + AllSubject + "</option>";
                for (int iIndex = 0; iIndex < dtLawDocumentType.Rows.Count; iIndex++)
                {
                    string LawDocumentTypeId = (string)dtLawDocumentType.Rows[iIndex]["LawDocumentTypeId"];
                    string Subject = (string)dtLawDocumentType.Rows[iIndex]["Subject"];
                    SelectLawDocumentType += "<option value=\"" + LawDocumentTypeId + "\">" + Subject + "</option>";
                }
                SelectLawDocumentType += "</select>";
                return SelectLawDocumentType;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
            return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
        }
    }
}